package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.ProductBean;
import com.project.bean.SellOrderBean;
import com.project.bean.UserBean;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @RequestMapping("addProduct")
    public String addProduct(ProductBean product, Integer buyNum, String flag, HttpServletRequest request, MultipartFile mf) throws IOException {
        UserBean userBean = (UserBean) request.getSession().getAttribute("loginUser");

        if (mf != null) {
            String fileName = null;
            if (mf.isEmpty() == false) {
                fileName = mf.getOriginalFilename();
                fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
                mf.transferTo(new File("D:\\langwo\\zuoye\\studentM\\codeM\\lastShopProject\\manage\\shopProject\\src\\main\\resources\\static\\headImg/" + fileName));
                product.setImgPath(fileName);
            }
        }
        productService.addProduct(product, userBean.getUserId(), buyNum, flag);
        return "success";
    }

    @RequestMapping("inStorage")
    public String InStorage(String supplierName, Integer loginUserId) {
        productService.InStorage(supplierName, loginUserId);
        return "success";
    }

    @RequestMapping("addProductInSellCar")
    public String addProductInSellCar(String productId, HttpServletRequest request) {
        UserBean userBean = (UserBean) request.getSession().getAttribute("loginUser");
        productService.addProductInSellCar(productId, userBean.getUserId());
        return "success";
    }

    @RequestMapping("OutStorage")
    public String OutStorage(SellOrderBean sellOrderBean, HttpServletRequest request) {
        UserBean userBean = (UserBean) request.getSession().getAttribute("loginUser");

        productService.OutStorage(sellOrderBean, userBean.getUserId());
        return "success";
    }

    @RequestMapping("findStorageByItem")
    public IPage<ProductBean> findStorageByItem(Integer pageNO, String productType, String productName, String isPrice, Integer startNum, Integer endNum) {
        return productService.findStorageByItem(pageNO, productType, productName, Integer.parseInt(isPrice), startNum, endNum);
    }

    @RequestMapping("findProductByInStorage")
    public ProductBean findProductByInStorage(Integer inStorageProductId) {
        return productService.findProductByInStorage(inStorageProductId);
    }

    @RequestMapping("updateInStorageProductPrice")
    public String updateInStorageProductPrice(String productId, Integer newPrice) {
        productService.updateInStorageProductPrice(productId, newPrice);
        return "success";
    }

    @RequestMapping("findProductType")
    public List<ProductBean> findProductType() {
        return productService.findProductType();
    }
}
